package com.Dao;

import cn.hutool.core.date.DateUtil;
import com.Bean.User;
import com.Common.Constant;
import com.Common.PageInfo;
import com.Util.JdbcUtil;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: hkw14
 * @time: 2021/4/28 17:33
 */
public class UserDao extends BaseDao{
    /**
     * @description: 获取用户用于登录验证
     * @Params: * @param userName
 * @param password
     * @return: com.Bean.User
     * @author: hkw14
     * @time: 2021/4/28 17:34
     */
    public User selectByAccountAndPwd(String userName, String password) {
        String sql = "SELECT * FROM `sys_user` WHERE userName = ? AND `password` = ?;";
        User user = this.selectOne(sql, User.class, userName,password);
        return user;
    }

    /**
     * @description: 分页查询
     * @Params: * @param params
     * @param page
     * @param limit
     * @return: PageInfo<User>
     * @author: hkw14
     * @time: 2021/4/28 17:36
     */
    public PageInfo<User> selectPage(Map<String,Object> params, Integer page, Integer limit) {
        String sql = "select * from `sys_user` where";
        //模糊查询
        if(params.containsKey("userName")) {
            sql = sql + "  userName like '%"+params.get("userName")+"%'   and";
        }
        if(params.containsKey("realName")) {
            sql = sql + "  realName like '%"+params.get("realName")+"%'   and";
        }
        if(params.containsKey("role")) {
            sql = sql + "  role= "+params.get("role")+" and";
        }
        if(sql.endsWith("and")) {
            sql = sql.substring(0,sql.lastIndexOf("and"));
        }
        if(sql.endsWith("where")) {
            sql = sql.substring(0,sql.lastIndexOf("where"));
        }
        return this.selectPage(sql, User.class, page, limit);
    }

    public boolean resetPassword(int id) {
        String sql = "update `sys_user` set password=? where ID=?";
        return this.update(sql, Constant.DEFAULT_PASSWORD,id);
    }

    public User selectByUserName(String userName) {
        String sql = "select * where userName=?";
        return this.selectOne(sql, User.class, userName);
    }

    public boolean insertUser(String userName,String password,String realName,Integer role) {
        String sql = "INSERT INTO  sys_user (userName, password, realName, role, isDel, createTime) VALUES (?,?, ?,?,1,?)";
        String createTime = DateUtil.format(new Date(),Constant.YYYY_MM_DD_HH_MM_SS);
        int userId = this.insert(sql,userName,password,realName,role,createTime);
        return userId > 0 ?true:false;
    }

    public boolean batchDelete(String[] ids) {
        Connection conn = JdbcUtil.getConn();
//        System.out.println(conn);
//        System.out.println(Arrays.toString(ids));
        PreparedStatement prep = null;
        String sql = "update sys_user set isDel = 2 where ID=?";

        try {
            //设置事务为手动提交
            conn.setAutoCommit(false);
            prep = conn.prepareStatement(sql);
            for (String id : ids) {
                prep.setInt(1, Integer.parseInt(id));
                prep.addBatch();
            }
            int[] count = prep.executeBatch();
            //手动提交事务
            conn.commit();
            return count.length > 0 ?true:false;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, prep);
        }
        return false;
    }


    public User selectById(int id) {
        String sql = "select * from sys_user where ID=?";
        User user = this.selectOne(sql, User.class, id);
        return user;
    }

    public boolean updatePwd(int id,String password) {
        String sql = "update sys_user set password=? where ID=?";
        return this.update(sql, password,id);
    }

    @Test
    public void Test2(){
        UserDao userDao = new UserDao();
        boolean b = userDao.batchDelete(new String[]{"1", "5", "7"});
        System.out.println(b);
    }
}
